/**
 * 文本切割
 * @param text 待切割的文本
 * @param len 文本总长度
 * @returns {string} 切割之后的文本
 */
function lineFeed(text,len) {
    var oldText = String(text);
    var newText = "";
    var index = 0;
    while (oldText.length >= len){
        newText = newText.concat(oldText.slice(index,index+len).concat("<br/>"));
        oldText = oldText.slice(index+len,text.length);
    }
    newText = newText.concat(oldText);
    return newText;
}

/**
 * 主页异步加载数据
 * @param type 加载数据类型
 * @param page 当前的页数
 * @param pageSize 每页显示的数量
 * @param pageCount 总条数
 * @param content 要创建的样式
 */
function loadShopAjax(type,page,pageSize,pageCount,content) {
    $.ajax({
        async: "false",
        url:"/commodity/list",
        type :"POST",
        data : {
            "page": window.page ++,
            "pageSize": pageSize,
            "count":pageCount,
            "type" : type
        },
        success: function (data) {
            window.pageCount = data['count'] || -1;
            var commodities = data['commodities'];
            var $goodList = $(".good_list");
            if(commodities.length == 0 || window.page >= 3){
                $("#load_dv").hide();
                //$(".end").show();
                return ;
            }
            $.each(commodities,function (index,item) {
               // item["title"] = lineFeed(item["title"],14);
                //var $newNode = $(".good_list_good:first-child").clone().css("display","block")
                const $newNode = $("<div class=\"good_list_good\">\n" +
                    "        <div class=\"good_list_top\">\n" +
                    "            <a href=\"#\"><img src=\"images/good1.jpg\"></a>\n" +
                    "        </div>\n" +
                    "        <div class=\"good_list_bottom\">\n" +
                    "            <div class=\"clearFloat\">\n" +
                    "            <div class=\"good_price\">&yen<i>18000</i></div>\n" +
                    "            <div class=\"good_sale\">付款人数1800</div>\n" +
                    "            </div>\n" +
                    "            <div class=\"good_l_info\">\n" +
                    "                <a href=\"\">\n" +
                    "                    <span>Canon/佳能M50 15-45套机微单相机入门</br>级数码高清旅游m50 eos女</span>\n" +
                    "                </a>\n" +
                    "            </div>\n" +
                    "        </div>\n" +
                    "    </div>");
                $newNode.find(".good_price i").text(item['price']).end()
                    .find(".good_l_info span").html(item["title"]).end()
                    .find(".good_list_top img").attr("src",item["images"]).end()
                    .find(".good_sale").html("付款人数"+item['salesVolume']).end()
                    .find("a").attr("href","/commodity/commodity_info/"+item['cid']).end();
                $newNode.appendTo($goodList);
                $(".toTop").fadeIn(5000);
                window.state = true;
            });
        }
    });
}


function conditionLoadIndex($btn,$lis,typeup,typedown){
    $lis = $lis || $(".good_list");
    $("#load_dv").show();
    $(".end").hide();
    if($btn.attr("asc") != undefined){
        window.type = type;
        $btn.removeAttr("asc");
        $lis.empty();

    }else{
        window.type = "type";
        $btn.attr("asc","asc");
        $lis.empty();
    }
    window.page = 1;
    window.pageCount = -1;
    loadShopAjax(type,page,pageSize,window.pageCount);
}

window.pageSize = 9;
window.type = "all";
window.state = true;
window.page = 1;
window.pageCount = -1;
// 注册滚动条事件
$(window).on("scroll",function () {
    // 判断搞滚动条是否已经到底部
    if( $(document).scrollTop() + $(window).height() > $(document).height() - 10) {
        if(state){
            window.state =false;
            // $("#load_dv").slideDown(1000);
            if(window.pageCount == -1 || window.pageCount > pageSize * page){
                 loadShopAjax(type,page,pageSize,window.pageCount)
            }else{
                window.state = true;
                $("#load_dv").hide();
                $(".end").show();
            }
        }

    }
});